import type { RouteMeta,RouteRecordRaw } from "vue-router";
import router from "../router/index";

type IRoute = RouteRecordRaw & {id?:string};

//定义全部路由：Admin下的子一级。
const allRoutes:IRoute[] = [
    {
        id:"1",
        name:"/",
        path:"",
        redirect:"/Admin/Home"
    },
    {
        id:"1",
        name:"Home",
        path:"Home",
        component:()=>import("../views/Admin/Home/Index.vue")
    },
    {
        id:"2-1",
        name:"QueryList",
        path:"QueryList",
        component:()=>import("../views/Admin/Goods/QueryList.vue")
    },
    {
        id:"2-2",
        name:"ListWithPage",
        path:"ListWithPage",
        component:()=>import("../views/Admin/Goods/ListWithPage.vue")
    },
    {
        id:"3-1",
        name:"BannerList",
        path:"BannerList",
        component:()=>import("../views/Admin/Banner/List.vue")
    },
    {
        id:"3-2",
        name:"BannerAdd",
        path: "BannerAdd",
        component: () => import("../views/Admin/Banner/Add.vue")
    },
    {
        id:"4-1",
        name:"AdminList",
        path:"AdminList",
        component:()=>import("../views/Admin/Admin/List.vue")
    },
    {
        id:"4-2",
        name:"AdminAdd",
        path:"AdminAdd",
        component:()=>import("../views/Admin/Admin/Add.vue")
    },
    {
        id:"5-1",
        name:"ShoppingCar",
        path:"ShoppingCar",
        component:()=>import("../views/Admin/ShoppingCar/Index.vue")
    },
    {
        id:"6-1",
        name:"Order",
        path:"Order",
        component:()=>import("../views/Admin/Order/Index.vue")
    },
    {
        id:"7-1",
        name:"User",
        path:"User",
        component:()=>import("../views/Admin/User/Index.vue")
    },
]

export default function getRoutes(){
    //1、获取当前用户的权限
    let checkedkeys:string[] = []
    const grants = sessionStorage.getItem("grants")
    if(grants){
        checkedkeys = JSON.parse(grants)
    }if(checkedkeys.length===0){
        checkedkeys = ['1','2','2-1','2-2','3','3-1','3-2','3-1-R','3-1-U','3-1-D','4-1-R','4-1-D','4-1-U','4','4-1','4-2','5','5-1','6','6-1','7','7-1']
    }

    //2、获取当前用户的路由配置

    allRoutes.forEach(item=>{
        if(checkedkeys.includes(item.id as string)){
            //动态添加路由配置：vueRouter对象.addRoute(父级路由的name，路由配置)

                let currRoute = {...item}

            let arr = ["0","0","0","0"];
            if(checkedkeys.includes(item.id+"-C")){
                arr[0] = "1"
            }
            if(checkedkeys.includes(item.id+"-R")){
                arr[1] = "1"
            }
            if(checkedkeys.includes(item.id+"-U")){
                arr[2] = "1"
            }
            if(checkedkeys.includes(item.id+"-D")){
                arr[3] = "1"
            }

            currRoute.meta = {
                crud:arr.join("")
            }
            // console.log("currRoute",currRoute);
            
            // console.log("currRoute.meta",currRoute.meta);
            
            router.addRoute("Admin",currRoute)
            // console.log("router",router);
            
            
        }
    })

    // console.log("router.getRoutes()",router.getRoutes());
    
}

export const clearCurrentRoutes = () =>{
    //1、获取当前用户的权限
    let checkedkeys:string[] = []
    const grants = sessionStorage.getItem("grants")
    if(grants){
        checkedkeys = JSON.parse(grants)
    }
    if(checkedkeys.length === 0){
        checkedkeys = ['1','2','2-1','2-2','3','3-1','3-2','4','4-1','4-2','5','5-1','6','6-1','7','7-1'];
    }

    //2、删除当前用户的路由配置

    allRoutes.forEach(item=>{
        if(checkedkeys.includes(item.id as string)){
            router.removeRoute(item.name as string)
        }
    })
}